﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RunningUIControll : MonoBehaviour
{
    [SerializeField]
    private Text scoreText;

    [SerializeField]
    private Button pauseButton;

    [SerializeField]
    private GameObject pauseUI;

    [SerializeField]
    private Button continueButton;

    // Start is called before the first frame update
    void Start()
    {
        //订阅分数变化事件
        GlobalEventManager.OnScoreChange += UpdateScore;

        //订阅重新开始事件
        GlobalEventManager.OnGameRestart += Restart;

        //订阅暂停事件
        GlobalEventManager.OnGamePause += Pause;

        //订阅继续事件
        GlobalEventManager.OnGameContinue += Continue;

        //设定按键行为
        pauseButton.onClick.AddListener(
            delegate
            {
                SendPauseRequest();
            }
            );
        continueButton.onClick.AddListener(
            delegate
            {
                SendContinueRequest();
            }
            );
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void UpdateScore(int score)
    {
        scoreText.text = "得分： " + score.ToString();
    }

    private void Restart()
    {
        scoreText.text = "得分： 0";
        pauseUI.SetActive(false);
        gameObject.SetActive(false);
    }

    private void SendPauseRequest()
    {
        GlobalEventManager.SendPauseRequest();
    }

    private void Pause()
    {
        pauseUI.SetActive(true);
    }

    private void SendContinueRequest()
    {
        GlobalEventManager.SendContinueRequest();
    }

    private void Continue()
    {
        pauseUI.SetActive(false);
    }
}
